package com.maas.util.mybatis.jpa.littlenb.support.template.insertex;

import com.littlenb.mybatisjpa.support.template.SqlTemplate;
import com.littlenb.mybatisjpa.util.PersistentUtil;
import com.maas.util.mybatis.jpa.littlenb.common.db.JbaDbPluginConst;
import org.apache.ibatis.jdbc.SQL;



public class SelectKeySqlTemplate  implements SqlTemplate {
    public static final SelectKeySqlTemplate INSTANCE = new SelectKeySqlTemplate();

    @Override
    public String parseSQL(final Class<?> type) {
        // columns
        StringBuilder xmlSql = new StringBuilder();
        String table =PersistentUtil.getTableName(type);


        String seq = "nextval('seq_rc_lib_code')";
        xmlSql.append("<selectKey resultType=\"Integer\" keyProperty=\"id\" order=\"BEFORE\">");
        if(JbaDbPluginConst.DIALECT_CURR.equals(JbaDbPluginConst.DIALECT_MYSQL)){
            seq = "nextval('seq_"+table+"')";
        }else{
            seq = "seq_"+table+".nextval";
        }
        String finalSeq = seq;
        String  sql =  new SQL() {
            {
                SELECT(finalSeq +"");
                FROM("dual");
            }
        }.toString();

        xmlSql.append(sql);
        xmlSql.append("</selectKey>");

        return xmlSql.toString();
    }
}
